package sv.gob.mh.dgii.dao.security;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import sv.gob.mh.dgii.dao.hibernate.HibernateDaoImpl;
import sv.gob.mh.dgii.model.AsOpcion;
import sv.gob.mh.dgii.model.AsRol;

@Repository
public class AsRolDaoImpl extends HibernateDaoImpl implements AsRolDao {

    public AsRolDaoImpl() {
        super(AsRol.class);
    }

    @Override
    public List<AsRol> findBySRolLike(String srol) {
        Session session = getSessionFactory().getCurrentSession();
        Query q = session.createQuery("select distinct r from AsRol r where r.srol like :srol order by r.srol");
        q.setParameter("srol", "%" + srol + "%");
        return q.list();
    }

    @Override
    public AsRol findBySRol(String srol) {
        return (AsRol) getSessionFactory().getCurrentSession().createQuery(
                "from AsRol as rol where rol.srol = :srol")
                .setParameter("srol", srol)
                .uniqueResult();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<AsRol> findAllBySRol(String[] authorities) {
        return (List<AsRol>) getSessionFactory().getCurrentSession().createQuery(
                "from AsRol as rol where rol.srol in (:srol)")
                .setParameterList("srol", authorities)
                .list();
    }

    @Override
    public List<AsRol> findByOpcion(AsOpcion opcion) {
        Session session = getSessionFactory().getCurrentSession();
        Query q = session.createQuery("select o.roles from AsOpcion o where o.copcion=:id ");
        q.setParameter("id", opcion.getCopcion());
        return q.list();
    }
}
//class